package sparkExamples.exerciseDemo

import org.apache.spark.{SparkConf, SparkContext}

object ExerciseDemo07 {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("ExerciseDemo").setMaster("local[*]")
    val sparkContext = new SparkContext(conf)

    val txtRDD = sparkContext.textFile("src\\main\\scala\\data\\exerciseData.txt")
    txtRDD.cache()

    /**
     * * 14、12班男生平均总成绩是多少？
     * * 15、12班女生平均总成绩是多少？
     * * 16、同理求13班相关成绩?
     */
    val nan12 = txtRDD.filter {
      line => {
        val strings = line.split(" ")
        if (strings(0) == "12" && strings(3) == "男") {
          true
        } else {
          false
        }
      }
    }
    val score_num: (Int, Int) = nan12.map {
      line => {
        val data = line.split(" ")
        (data(5).toInt, 1)
      }
    }.reduce((x, y) => ((x._1 + y._1), (x._2 + y._2)))
    println("12班男生平均分：" + (score_num._1/score_num._2))

    sparkContext.stop()
  }

}
